package com.zqp.web.intercepter;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
 * 
* @author zqp
* @version2017年12月19日 
* @description
 */
//@Component
public class TimerIntercepter implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse arg1, Object arg2, Exception ex)
			throws Exception {
		System.out.println("afterCompletion");
		long  startTime = (long) request.getAttribute("startTime");
		
		System.out.println("time intercepter 耗时"+ (new Date().getTime() - startTime));
		
		System.out.println("ex is  " + ex);

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
	System.out.println("postHandle");
	long  startTime = (long) request.getAttribute("startTime");
	
	System.out.println("time intercepter 耗时"+ (new Date().getTime() - startTime));
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		System.out.println("preHandle");
		System.out.println(((HandlerMethod)object).getBean().getClass().getName());
		System.out.println(((HandlerMethod)object).getMethod().getName());
		
		request.setAttribute("startTime", new Date().getTime());
		return true;
	}

}
